#include "stdafx.h"
#include "./tmApplication.h"
#include "./tmMainWindow.h"
#include "./tmMsgDispatcher.h"

VOID    onTimerProc (VOID* pUser)
{
	tmApplication* app = (tmApplication*)pUser;
	app->onTimer();
}

BOOL tmApplication::initialise( HWND hwnd,RectI& rect )
{
	E_Return(GLES::CreateGLContext(hwnd));
	_timer.CreateTimer(1,onTimerProc,5,this);
	_pWindow = new tmMainWindow();
	Return(_pWindow->createFrom(hwnd,rect));
}

tmApplication::tmApplication():
_pWindow(NULL)
{

}

tmApplication::~tmApplication()
{
	SAFE_DELETE(_pWindow);
}

VOID tmApplication::drawWindow()
{
	_pWindow->draw(NULL);
}

VOID tmApplication::Finalise()
{

}

VOID tmApplication::onTimer()
{
	_pWindow->onTimer(&_timer);
	BOOL redraw = GLES::Singleton<tmMsgDispatcher>::GetReference().getNeedRedraw();
	if(redraw)
	{
		::InvalidateRect(GLES::getContext()->m_hwnd,NULL,false);
	}
}

VOID tmApplication::onInput( int x,int y,InputType state )
{
	_pWindow->onInput(x,y,state);
	BOOL redraw = GLES::Singleton<tmMsgDispatcher>::GetReference().getNeedRedraw();
	if(redraw)
	{
		::InvalidateRect(GLES::getContext()->m_hwnd,NULL,false);
	}
}

VOID tmApplication::finalise()
{

}